import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * Problem Id: 1009
 */
public class MainTest {
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args)throws Exception{
		Scanner in = new Scanner(System.in);
		int width = 0,pixel,length;
		int row = 1, col = 1, height = 0, total = 0;
		int E=0, W=0, S=0, N=0, EN=0, ES=0, WS=0, WN=0;
		// store RLE pairs
		List<Pair> list = new ArrayList<Pair>();
		// store output image RLE pairs
		List<Pair> result = new ArrayList<Pair>();
		while(true){
			// the width of image
			width = in.nextInt();
			if (width == 0){
				break;
			}
			// read all RLE pairs
			total = 0;
			while(true){
				pixel = in.nextInt();
				length = in.nextInt();
				if (length == 0){
					break;
				}
				list.add(new MainTest().new Pair(pixel,length));
				total += length;
			}
			// calculate the output image
			row = 1; col = 1; height = total/width;		
			for(int i=0;i<list.size();i++){
				Pair pair = list.get(i);
				for(int k=0;k<pair.length;k++){
					E=0; W=0; S=0; N=0; EN=0; ES=0; WS=0; WN=0;
					// E
					if (col < width && k == (pair.length -1)){
						// next pixel
						Pair pairE = list.get(i+1);
						E = Math.abs(pair.pixel - pairE.pixel);
					}
					// W
					if (col > 1 && k == 0){
						Pair pairW = list.get(i-1);
						W = Math.abs(pair.pixel - pairW.pixel);
					}
					// S
					if (row < height && (pair.length-1-k) < width){
						Pair pairS = null;
						int sum = pair.length - 1-k;
						for (int idx = i+1; idx<list.size(); idx++){
							sum += list.get(idx).length;
							if (sum >= width){
								pairS = list.get(idx);
								break;
							}
						}
						S = Math.abs(pair.pixel - pairS.pixel);
					}
					// N
					if (row > 1 && k < width){
						Pair pairN = null;
						int sum = k;
						for(int idx=i-1;idx>=0;idx--){
							sum += list.get(idx).length;
							if(sum >= width){
								pairN = list.get(idx);
								break;
							}
						}
						N = Math.abs(pair.pixel - pairN.pixel);
					}
					// EN
					if (col < width && row > 1 && (k+1) < width){
						Pair pairEN = null;
						int sum = k+1;
						for(int idx=i-1;idx>=0;idx--){
							sum += list.get(idx).length;
							if(sum >= width){
								pairEN = list.get(idx);
								break;
							}
						}
						EN = Math.abs(pair.pixel - pairEN.pixel);
					}
					// ES
					if (col < width && row < height && (pair.length-1-k) < (width+1)){
						Pair pairES = null;
						int sum = pair.length - 1-k;
						for (int idx = i+1; idx<list.size(); idx++){
							sum += list.get(idx).length;
							if (sum >= (width+1)){
								pairES = list.get(idx);
								break;
							}
						}
						ES = Math.abs(pair.pixel - pairES.pixel);
					}
					// WS
					if (col > 1 && row < height && (pair.length-1-k) < (width-1)){
						Pair pairWS = null;
						int sum = pair.length - 1-k;
						for (int idx = i+1; idx<list.size(); idx++){
							sum += list.get(idx).length;
							if (sum >= (width-1)){
								pairWS = list.get(idx);
								break;
							}
						}
						WS = Math.abs(pair.pixel - pairWS.pixel);
					}
					// WN
					if (col > 1 && row > 1 && k < (width+1)){
						Pair pairWN = null;
						int sum = k;
						for(int idx=i-1;idx>=0;idx--){
							sum += list.get(idx).length;
							if(sum >= (width+1)){
								pairWN = list.get(idx);
								break;
							}
						}
						WN = Math.abs(pair.pixel - pairWN.pixel);
					}
					// find max
					int max = E;
					if (max < W){max =W;}
					if (max < S){max =S;}
					if (max < N){max =N;}
					if (max < EN){max = EN;}
					if (max < ES){max = ES;}
					if (max < WS){max = WS;}
					if (max < WN){max = WN;}
					if (result.size() < 1){
						result.add(new MainTest().new Pair(max,1));
					} else {
						Pair previous = result.get(result.size()-1);
						if (previous.pixel == max){
							previous.length++;
						} else {
							result.add(new MainTest().new Pair(max,1));
						}
					}
					// 处理大量相同数值的情况,中间可跳过多个值为0的情况					
					if (max == 0 && k > width){
						int step = (pair.length-1-k)-(width+1);
						if (step > 0){
							Pair tmp = result.get(result.size()-1);
							tmp.length += step;
							k = k+step;
							// 
							row += (col + step)/width;
							col = (col + step)%width;
						}
					}
					// 转到下一行
					if (col == width){
						col = 1;row++;
					} else {
						col++;
					}
				}
			}
			// output
			System.out.println(width);
			for(int i=0;i<result.size();i++){
				System.out.println(result.get(i).toString());
			}
			System.out.println("0 0");
			// clear list before next image
			list.clear();
			result.clear();
		}
		System.out.println("0");
	}
	class Pair{
		public int pixel;
		public int length;
		public Pair(int pixel,int length){
			this.pixel = pixel;
			this.length = length;
		}
		public String toString() {
			return pixel + " " + length;
		}
	}
}